package ycl.important.controller.security;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import ycl.important.common.ApiResult;
import ycl.important.common.models.req.base.AccountPassReq;
import ycl.important.common.models.res.login.LoginRes;
import ycl.important.common.models.req.login.RegisterReq;
import ycl.important.common.models.req.login.SmsLoginReq;
import ycl.important.service.login.LoginService;
import ycl.important.service.sys.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author: YCL
 * @date: 2022/01/15 20:48:17
 */
@RestController
@RequestMapping("/security")
@Api(tags = "权限")
@ApiSort(21)
public class SecurityController {
	@Resource
	private LoginService loginService;
	@Resource
	private UserService userService;

	@GetMapping("/login/isLogin")
	@ApiOperation("查询用户是否登录")
	@ApiOperationSupport(order = 1)
	public ApiResult<Object> isLogin(HttpServletRequest request) {
		boolean isLogin = userService.isLogin(request);
		return isLogin ? ApiResult.success() : ApiResult.fail(null);
	}

	@PostMapping("/login/register")
	@ApiOperation("用户注册")
	@ApiOperationSupport(order = 11)
	public ApiResult<Object> register(@RequestBody @Valid RegisterReq dto) {
		loginService.register(dto);
		return ApiResult.success();
	}


	@PostMapping("/login/usernamePass")
	@ApiOperation("用户名密码登录")
	@ApiOperationSupport(order = 21)
	public ApiResult<LoginRes> loginByUsernamePassword(@RequestBody @Valid AccountPassReq dto) {
		LoginRes vo = loginService.loginByUsernamePassword(dto);
		return ApiResult.success(vo);
	}

	@PostMapping("/login/mobilePass")
	@ApiOperation("手机号密码登录")
	@ApiOperationSupport(order = 22)
	public ApiResult<LoginRes> loginByMobilePassword(@RequestBody @Valid AccountPassReq dto) {
		LoginRes vo = loginService.loginByMobilePassword(dto);
		return ApiResult.success(vo);
	}

	@PostMapping("/login/sms")
	@ApiOperation("短信登录")
	@ApiOperationSupport(order = 23)
	public ApiResult<LoginRes> loginBySms(@RequestBody @Valid SmsLoginReq dto) {
		LoginRes vo = loginService.loginBySms(dto);
		return ApiResult.success(vo);
	}

	@GetMapping("/separate")
	@ApiOperation("分隔符 ===========")
	@ApiOperationSupport(order = 100)
	public void separate() {
	}

	@PostMapping("/logout")
	@ApiOperation("退出登录")
	@ApiOperationSupport(order = 101)
	public ApiResult<Object> logout(HttpServletRequest request) {
		loginService.logout(request);
		return ApiResult.success();
	}
}
